<template>
  <el-card shadow="hover">
    <el-table :data="tableData" style="width: 100%; margin-bottom: 20px" height="480" row-key="permissionId" border>
      <el-table-column prop="faultId" label="故障编号" sortable> </el-table-column>
      <el-table-column prop="bedId" label="出现故障床编号" sortable> </el-table-column>
      <el-table-column prop="faultName" label="故障名称" sortable> </el-table-column>
      <el-table-column prop="faultStatus" label="故障状态" sortable> </el-table-column>
      <el-table-column prop="faultTime" label="故障时间" sortable> </el-table-column>
      <el-table-column prop="empId" label="维修人员Id" sortable> </el-table-column>

      <el-table-column label="操作" fixed="right">
        <template slot-scope="scope">
          <el-tooltip content="分配维修人员" placement="bottom" effect="light">
            <el-button type="primary" @click="distributeUI(scope.row)" size="mini" icon="el-icon-setting" plain circle></el-button>
          </el-tooltip>

          <el-tooltip content="编辑" placement="bottom" effect="light">
            <el-button type="info" @click="updateUI(scope.row)" size="mini" circle plain icon="el-icon-edit">
            </el-button>
          </el-tooltip>

          <el-tooltip content="删除" placement="bottom" effect="light">
            <el-button type="danger" @click="del(scope.row)" size="mini" circle plain icon="el-icon-delete"></el-button>
          </el-tooltip>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog
        title="分配维修人员"
        :visible.sync="dialogVisible"
        width="30%"
        :before-close="handleClose">
        <el-form :model="form">
       
          <el-form-item label="维修人员：" :label-width="formLabelWidth">
            <el-select v-model="form.empId" placeholder="请选择维修人员" >
              <el-option v-for="item in options" :key="item.empId" :label="item.empName"
                :value="item.provinceId">
              </el-option>
            </el-select>
          </el-form-item>
        </el-form>

        
        <span slot="footer" class="dialog-footer">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="distribute">确 定</el-button>
        </span>
    </el-dialog>


    <el-dialog title="故障" :visible.sync="dialogFormVisible">
      <el-form :model="form">
        <el-form-item label="故障状态" :label-width="formLabelWidth">
          <el-select v-model="form.faultStatus" placeholder="请选择">
            <el-option label="已修复" value="已修复">
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="saveOrUpdate()">确 定</el-button>
      </div>
    </el-dialog>




    <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="pageInfo.page"
      :page-sizes="[5, 10, 15, 20]" :page-size="pageInfo.size" layout="total, sizes, prev, pager, next, jumper"
      :total="pageInfo.total">
    </el-pagination>
  </el-card>
</template>

<script>
import { findAll, doSaveOrUpdate, dodel,findDistributeEmp,doDistribute } from "@/api/fault/index.js";

export default {
  mounted() {
    this.updateTable();
    this.getAll()
  },
  data() {
    return {
      tableData: [],
      fileList: [],
      form: {},
      dialogFormVisible: false,
      dialogVisible: false,
      formLabelWidth: "180px",
      pageInfo: {
        total: 0,
        page: 1,
        size: 5,
      },
      options: [],
    };
  },
  methods: {
    handleClose(){

    },
    distribute(){
      console.log(this.form);
       doDistribute(this.form).then(r=>{
        this.dialogVisible = false;
        if (r.code == 200) {
              this.updateTable();
              this.$message.success(r.msg);
              return;
            }
      });
    },
    distributeUI(row){
      this.dialogVisible = true;
      this.form = row;
      //查询所有当地维修人员
      findDistributeEmp(row.bedId).then(r=>{
        this.options = r.data;
      })
    },
    async getAll() {
      let res = await findAll(this.pageInfo)
      this.tableData = res.data
      console.log(res);
    },
    updateUI(row) {
      this.dialogFormVisible = true;
      this.form = row;
    },
    del(row) {
      this.$confirm('确认删除?')
        .then(() => {
          dodel(row.faultId).then(r => {
            if (r.code == 200) {
              this.updateTable();
              this.$message.success(r.msg);
              return;
            }
            this.$message.error(r.msg);
          });
        })
        .catch(() => {
        });
    },
    handleSizeChange(size) {
      this.pageInfo.size = size;
      this.updateTable();
    },
    handleCurrentChange(page) {
      this.pageInfo.page = page;
      this.updateTable();
    },
    updateTable() {
      findAll(this.pageInfo).then((r) => {
        this.tableData = r.data.list;
        this.pageInfo.total = r.data.total;
      });
    },
    saveOrUpdate() {
      doSaveOrUpdate(this.form).then(r => {
        if (r.code == 200) {
          this.$message.success(r.msg);
          this.updateTable();
          // TODO 添加维修记录到维修表中
          // TODO 删除该记录
          this.fileList = [];
          this.dialogFormVisible = false;
        }
      })
    },
    uploadSuccess(r) {
      this.form.photo = r.data.newFileName;
    },
    // distRoleToUser(row) {
    //   this.$router.push(`DistRoleToUser/${row.userId}`);
    // }
  },
};
</script>

<style></style>